package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.domain.Company;
import com.ruoyi.system.domain.StockLog;
import com.ruoyi.system.service.ICompanyService;
import com.ruoyi.system.service.IStockLogService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Stock;
import com.ruoyi.system.service.IStockService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存Controller
 * 
 * @author ruoyi
 * @date 2025-09-01
 */
@RestController
@RequestMapping("/system/stock")
public class StockController extends BaseController
{
    @Autowired
    private IStockService stockService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IStockLogService stockLogService;

    /**
     * 初始化库存
     * @return
     */
    @RequestMapping("/initStock")
    public AjaxResult initStock(@RequestBody JSONObject data){
        AjaxResult result = stockService.initStock(data);

        return result;
    }

    /**
     * 库存盘点
     * @param id
     * @param goodsNum
     * @param inOut
     * @return
     */
    @RequestMapping("/inventory")
    public AjaxResult inventory(@RequestParam("id") Long id,@RequestParam("goodsNum") double goodsNum,@RequestParam("inOut") String inOut,@RequestParam("remark") String remark){
        Stock stock = stockService.selectStockById(id);
        Company company = companyService.selectCompanyById(stock.getCompanyId());
        StockLog stockLog = new StockLog();
        stockLog.setType("4");//盘点
        stockLog.setGoodsName(stock.getGoodsName());
        stockLog.setInOut(inOut);
        stockLog.setGoodsid(stock.getGoodsId());
        stockLog.setCompanyId(stock.getCompanyId());
        stockLog.setCompanyName(company.getCompany());
        stockLog.setGoodsNum(inOut.equals("1")?goodsNum:(-1)*goodsNum);
        stockLog.setShopId(stock.getShopId());
        stockLog.setShopName(stock.getShopName());
        stockLog.setRemark(remark);

        int result = stockLogService.insertStockLog(stockLog);
        return AjaxResult.success(result);
    }

    /**
     * 查询库存列表
     */
    @PreAuthorize("@ss.hasRole('company')")
    @GetMapping("/list")
    public TableDataInfo list(Stock stock)
    {
        startPage();
        List<Stock> list = stockService.selectStockList(stock);
        return getDataTable(list);
    }

    /**
     * 导出库存列表
     */
    @PreAuthorize("@ss.hasPermi('system:stock:export')")
    @Log(title = "库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Stock stock)
    {
        List<Stock> list = stockService.selectStockList(stock);
        ExcelUtil<Stock> util = new ExcelUtil<Stock>(Stock.class);
        util.exportExcel(response, list, "库存数据");
    }

    /**
     * 获取库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:stock:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stockService.selectStockById(id));
    }

    /**
     * 新增库存
     */
    @PreAuthorize("@ss.hasPermi('system:stock:add')")
    @Log(title = "库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Stock stock)
    {
        return toAjax(stockService.insertStock(stock));
    }

    /**
     * 修改库存
     */
    @PreAuthorize("@ss.hasPermi('system:stock:edit')")
    @Log(title = "库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Stock stock)
    {
        return toAjax(stockService.updateStock(stock));
    }

    /**
     * 删除库存
     */
    @PreAuthorize("@ss.hasPermi('system:stock:remove')")
    @Log(title = "库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stockService.deleteStockByIds(ids));
    }
}
